package org.dromara.oce.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.Locale;

/**
 * @author Pippin
 * @version 1.0
 * @date 2024/12/24 13:43
 */
public class TimeUtils {

    public static long getSecondsByDateStr(String dateStr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(dateStr);
            return date.getTime() / 1000;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 取出多余的分钟、秒数（01:12:12变成01:00:00）
     *
     * @param timestamp 秒级时间戳
     * @return 整数分钟的秒级时间戳
     */
    public static Long getHoursTimestamp(Long timestamp) {
        return timestamp - timestamp % 3600;
    }

    /**
     * 获取时间戳中的 小时
     *
     * @param timestampSecond 秒级时间戳
     * @return 小时数（24小时制）
     */
    public static int getHours(Long timestampSecond) {
        Date date = new Date(timestampSecond * 1000);
        return DateUtil.hour(date, true);
    }

    /**
     * 字符串日期（yyyy-MM-dd）转 秒级时间戳
     *
     * @param strDate yyyy-MM-dd
     * @return 当天秒级时间戳
     */
    public static Long getTimestampSecond(String strDate) {
        long timestamps = 0L;
        try {
            LocalDate parse = LocalDate.parse(strDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            timestamps = parse.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
            return timestamps;
        } catch (Exception e) {
            System.out.println("时间转化异常");
            return timestamps;
        }
    }

    /**
     * 获取当前时间的时间戳 (精确到小时)   yyyy-MM-dd HH:00:00
     *
     * @return 秒级时间戳（yyyy-MM-dd HH）
     */
    public static long getCurrHoursTimestampSecond() {
        String format = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:00:00");
        DateTime of = DateTime.of(format, "yyyy-MM-dd HH:mm:ss");
        return of.getTime() / 1000;
    }

    /**
     * 获取当前时间前一小时的时间戳 (精确到小时)   yyyy-MM-dd HH:00:00
     *
     * @return 秒级时间戳（yyyy-MM-dd HH）
     */
    public static long getBeforeHoursTimestampSecond() {
        String format = DateUtil.format(LocalDateTime.now(ZoneOffset.of("+8")).minusHours(1), "yyyy-MM-dd HH:00:00");
        DateTime of = DateTime.of(format, "yyyy-MM-dd HH:mm:ss");
        return of.getTime() / 1000;
    }


    /**
     * 获取当前时间的时间戳 (精确到小时)   yyyy-MM-dd HH:00:00
     *
     * @return 秒级时间戳（yyyy-MM-dd HH）
     */
    public static long getHoursTimestamp(String dateStr) {
        LocalDateTime parse = LocalDateTimeUtil.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
        String format = DateUtil.format(parse, "yyyy-MM-dd HH:00:00");
        DateTime of = DateTime.of(format, "yyyy-MM-dd HH:00:00");
        return of.getTime() / 1000;
    }

    /**
     * 获取当前时间的秒级时间戳 (精确到分钟)
     *
     * @return 秒级时间戳（yyyy-MM-dd HH:mm:00）
     */
    public static Long getCurrMinutesTimestampSecond() {
        String strDate = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:00");
        return getMinutesTimestampSecond(strDate);
    }

    /**
     * 根据传入时间获取秒级时间戳 (精确到分钟)   yyyy-MM-dd HH:mm:00
     *
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return 秒级时间戳（yyyy-MM-dd HH:mm:00）
     */
    public static Long getMinutesTimestampSecond(String strDate) {
        LocalDateTime parse = LocalDateTimeUtil.parse(strDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00"));
        String format = DateUtil.format(parse, "yyyy-MM-dd HH:mm:00");
        DateTime of = DateTime.of(format, "yyyy-MM-dd HH:mm:ss");
        return of.getTime() / 1000;
    }

    /**
     * 秒级时间戳 转 时间str
     *
     * @param timestampSecond 秒级时间戳
     * @return 字符串 yyyy-MM-dd HH:mm（精确到分钟）
     */
    public static String getStrHoursDate(Long timestampSecond) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestampSecond), ZoneOffset.ofHours(8));
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00"));
    }

    /**
     * 根据传入时间获取小时数   yyyy-MM-dd HH:mm:00
     *
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return 小时数
     */
    public static Integer getHoursByStr(String strDate) {
        LocalDateTime parse = LocalDateTimeUtil.parse(strDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return parse.getHour();
    }

    public static void main(String[] args) {
//        int year = 2025;
//        int month = 1;
//        int day = 1;
//        LocalDate localDate = LocalDate.of(year, month, day);
//        System.out.println(localDate.get(WeekFields.of(Locale.CHINA).weekOfWeekBasedYear()));
//        // 获取当前周的周一
//        LocalDate monday = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
//        System.out.println(monday);
//        // 获取当前周的周日
//        LocalDate sunday = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
//        System.out.println(sunday);
//        String date = "2025-02-18";
//        LocalDate localDate = LocalDate.parse(date);
//        int week = localDate.get(WeekFields.of(Locale.CHINA).weekOfWeekBasedYear());
//        System.out.println(week);
        Long hour = 1741050000L;
        System.out.println(getStrHoursDate(hour));

        System.out.println(getBeforeHoursTimestampSecond());
        String format = DateUtil.format(LocalDateTime.parse("2025-03-05 00:00:00",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00")).minusHours(1), "yyyy-MM-dd HH:00:00");
        System.out.println(format);

    }
}
